#!/usr/bin/make -f

BUILDER_IMAGES=		\
	centos-6 	\
	centos-7 	\
	fedora-30	\
	debian-jessie 	\
	debian-stretch	\
	debian-buster	\
	ubuntu-xenial	\
	ubuntu-trusty	\
	ubuntu-bionic \
	ubuntu-eoan \
	ubuntu-focal \
	kira

IMAGES=${BUILDER_IMAGES} devshell
DEFAULT_IMAGE ?= ubuntu-bionic
TARBALL_IMAGE ?= ubuntu-bionic
DEFAULT_DEB_IMAGE=$(DEFAULT_IMAGE)
DEFAULT_RPM_IMAGE=centos-7
DOCKER=docker
VERSION=$(shell scripts/version.sh)
DOCKER_RUN_ARGS=-e USER_NAME_ON_HOST=$(shell whoami)	\
        --network=host --privileged \
	--ulimit nofile=1024:1024 \
	-v $(ROOT_DIR):/source \
	-v $(DBLD_DIR)/build:/build \
	-v $(DBLD_DIR)/install:/install \
	-e CONFIGURE_OPTS="$${CONFIGURE_OPTS:-$(CONFIGURE_OPTS)}" \
	-e CCACHE_DIR=/build/ccache \
	-e VERSION=$(VERSION) \
	-e PATH=/usr/lib/ccache:/usr/lib64/ccache:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
	-e GRADLE_USER_HOME=/build/gradle-home \
	-e GRADLE_PROJECT_CACHE_DIR=/build/gradle-cache \
	-e GRADLE_FLAGS=--build-cache \
	$(if $(wildcard ${HOME}/.gitconfig),-v ${HOME}/.gitconfig:${HOME}/.gitconfig)
ROOT_DIR=$(shell pwd)
DBLD_DIR=$(ROOT_DIR)/dbld
BUILD_DIR=$(DBLD_DIR)/build
RELEASE_DIR=$(DBLD_DIR)/release
TARBALL=$(BUILD_DIR)/syslog-ng-$(VERSION).tar.gz
MODE=snapshot
CONFIGURE_OPTS=--enable-debug --enable-manpages --with-python=2 --prefix=/install $(CONFIGURE_ADD)

DOCKER_SHELL=$(DOCKER) run $(DOCKER_RUN_ARGS) --rm -ti balabit/syslog-ng-$* /source/dbld/shell

-include dbld/rules.conf

help:
	@echo "This script allows you to build release/snapshot artifacts, such "
	@echo "as rpm, deb packages and tarballs in controlled environments (e.g. "
	@echo "within docker containers)."
	@echo ""
	@echo "Please create the required docker image first using the 'image' target"
	@echo "then you can build syslog-ng binaries or do development using the "
	@echo "targets described below."
	@echo ""
	@echo "Supported OSs are: $(IMAGES)"
	@echo ""
	@echo "Packaging targets:"
	@echo "  deb: generate debs in dbld/build for $(DEFAULT_DEB_IMAGE)"
	@echo "  rpm: generate RPMs in dbld/build for $(DEFAULT_RPM_IMAGE)"
	@echo "  deb-<os>: generate debs in dbld/build for the specified OS"
	@echo "  rpm-<os>: generate rpms in dbld/build for the specified OS"
	@echo ""
	@echo "Docker images:"
	@echo "  image: generate the default docker image"
	@echo "  image-<os>: generate docker image with a specific OS for building syslog-ng"
	@echo "  images: generate all docker images"
	@echo ""
	@echo "Development:"
	@echo "  bootstrap: bootstrap source from a git clone (e.g. autogen.sh and friends)"
	@echo "  shell: start a shell in the specified OS"
	@echo "  login: start a 2nd shell alongside an already running one"
	@echo "  root-login: start a 2nd shell (as root) alongside an already running one"
	@echo "  tarball: generate a tarball in a controlled environment (e.g. docker)"
	@echo "  release: generate a release deb package in a controlled environment (e.g. docker)"
	@echo ""
	@echo "Notable Make variables:"
	@echo "  CONFIGURE_ADD -- add this to the configure command line in bootstrap"
	@echo "  DEFAULT_IMAGE -- override the default image, currently $(DEFAULT_IMAGE)"
	@echo "  TARBALL_IMAGE -- override the default image where the tarball is generated, currently $(TARBALL_IMAGE)"
	@echo ""

bootstrap: bootstrap-$(DEFAULT_IMAGE)
bootstrap-%: setup
	$(DOCKER) run $(DOCKER_RUN_ARGS) --rm  -i  balabit/syslog-ng-$* /source/dbld/bootstrap

make-%: setup
	$(DOCKER) run $(DOCKER_RUN_ARGS) --rm -i  balabit/syslog-ng-$(DEFAULT_IMAGE) /source/dbld/make $(MAKE_ARGS) $*

tarball: tarball-$(TARBALL_IMAGE)
tarball-%: setup
	@if [ -f $(TARBALL) ]; then \
		count=`find $(ROOT_DIR) -newer $(TARBALL) | sed -e 's,^$(ROOT_DIR),,' | grep -v -f $(DBLD_DIR)/tarball-changes.ignore | tee $(BUILD_DIR)/tarball-changed-files.txt | wc -l`; \
	else \
		echo "Tarball $(TARBALL) not found" > $(BUILD_DIR)/tarball-changed-files.txt; \
		count=1; \
	fi; \
	if [ "$${count}" -gt 0 ]; then \
		echo "Rebuilding, because these contents have changed since the tarball" && \
		cat $(BUILD_DIR)/tarball-changed-files.txt && \
		echo "Git status follows:" && \
		git status && \
		$(DOCKER) run $(DOCKER_RUN_ARGS) --rm -i balabit/syslog-ng-$* /source/dbld/tarball; \
	fi


pkg-tarball: pkg-tarball-$(TARBALL_IMAGE)
pkg-tarball-%: tarball-%
	$(DOCKER) run $(DOCKER_RUN_ARGS) --rm -i balabit/syslog-ng-$* /source/dbld/pkg-tarball $(MODE)

package: package-$(DEFAULT_IMAGE)
package-%: pkg-tarball
	$(DOCKER) run $(DOCKER_RUN_ARGS) --rm -i  balabit/syslog-ng-$* /source/dbld/package

deb: deb-$(DEFAULT_DEB_IMAGE)
deb-%: pkg-tarball
	$(DOCKER) run $(DOCKER_RUN_ARGS) --rm -i  balabit/syslog-ng-$* /source/dbld/deb

rpm: rpm-$(DEFAULT_RPM_IMAGE)
rpm-%: pkg-tarball
	$(DOCKER) run $(DOCKER_RUN_ARGS) --rm -i  balabit/syslog-ng-$* /source/dbld/rpm


release: MODE=release
release: deb

clean:
	rm -rf $(BUILD_DIR)/*

run: run-$(DEFAULT_IMAGE)
run: RUN_COMMAND=echo Specify RUN_COMMAND to do something sensible here
run-%: setup
	$(DOCKER) run $(DOCKER_RUN_ARGS) --rm -ti balabit/syslog-ng-$* bash -c "$(RUN_COMMAND)"

shell: shell-$(DEFAULT_IMAGE)
shell-%: setup
	$(DOCKER_SHELL)


images: $(foreach image,$(IMAGES), image-$(image))
image: image-$(DEFAULT_IMAGE)
image-%:
	cd $(DBLD_DIR)/images/ && $(DOCKER) build --build-arg=OS_PLATFORM=$* --build-arg=COMMIT=$$(git rev-parse --short HEAD) --network=host -t balabit/syslog-ng-$* -f $*.dockerfile .

pull-images: $(foreach image,$(BUILDER_IMAGES), pull-image-$(image))
pull-image: pull-image-$(DEFAULT_IMAGE)
pull-image-%:
	$(DOCKER) pull balabit/syslog-ng-$*

cache-image-%:
	IMAGE=$$(docker images -q balabit/syslog-ng-$*:latest | head -1);						\
	if [ "$$IMAGE" = "" ]; then 											\
		$(MAKE) -f $(DBLD_DIR)/rules pull-image-$*;								\
	fi;														\
	IMAGE=$$(docker images -q balabit/syslog-ng-$*:latest | head -1);						\
	if [ "$$IMAGE" != "" ]; then 											\
		image_commit=$$(docker inspect  --format '{{ index .Config.Labels "COMMIT"}}' $$IMAGE);			\
		dbld_changes=$$( 											\
			(git cat-file -e $${image_commit:-NO_COMMIT_LABEL_IN_DBLD_IMAGE}^{commit} 			\
				&& git diff $${image_commit} -- dbld | wc -l						\
				|| echo 1)										\
		);													\
	else														\
		dbld_changes=1; 											\
	fi;														\
	if [ "$${dbld_changes}" -gt 0 ]; then									\
		echo "$IMAGE: triggering rebuild, as dbld directory changed since the build of the image, or the cached image has no COMMIT label, or couldn't fetch from dockerhub" &&	\
		$(MAKE) -f $(DBLD_DIR)/rules image-$*;									\
	else 														\
		echo "DBLD version in image matches, not initiating rebuild...";					\
	fi

exec: exec-$(DEFAULT_IMAGE)
exec: EXEC_COMMAND=echo Specify EXEC_COMMAND to do something sensible here
exec-%: setup
	@container=`$(DOCKER) ps | grep syslog-ng-$* | head -1 | cut -d ' ' -f1`; \
	$(DOCKER) exec -ti  $$container $(EXEC_COMMAND)

login: login-$(DEFAULT_IMAGE)
login-%: EXEC_COMMAND=gosu $(shell whoami) /source/dbld/shell
login-%: exec-%
	@true

root-login: root-login-$(DEFAULT_IMAGE)
root-login-%: EXEC_COMMAND=bash
root-login-%: exec-%
	@true

setup:
	@mkdir -p dbld/build || true
	@mkdir -p dbld/install || true
	@mkdir -p dbld/release || true

list-builder-images:
	@echo ${BUILDER_IMAGES}
